"""
    This file is part of django-simp.

    django-simp is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    django-simp is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with django-simp.  If not, see <http://www.gnu.org/licenses/>.
"""
from PIL import Image 

from django.http import Http404
from django.template.loader import render_to_string

from simp.models import Preset, Module, ModulePresetBase

from django.utils import simplejson

class ModPreset(ModulePresetBase):
    hold_aspect = False
    max = {'width': 5000,'height': 3000}
    min = {'width': 50,'height': 50}
        
def get_toolbar():
    vals = {"label": "resize"}
    vals["has_presets"] = False
    presetQuery = Preset.objects.filter(module="resize")
    if presetQuery.count() > 1:
        vals["has_presets"] = True
        presetQuery = presetQuery.exclude(order=0)
        vals["presets"] = []
        for preset in presetQuery:
            presetVal = {}
            presetVal["label"] = preset.label
            presetVal["icon_class"] = "simp_resize"
            presetVal["on_click"] = render_to_string('simp/scripts/dyn_onclick.js', {'order': preset.order, 'mod': "resize" })
            vals["presets"].append(presetVal)
    vals["icon_class"] = "simp_crop"
    vals["on_click"] = render_to_string('simp/scripts/dyn_onclick.js', {'order': 0, 'mod': 'resize'})
    return render_to_string('simp/module_toolbar.html', vals)
    
def get_scripts():
    return render_to_string('simp/scripts/resize.js', {})

def get_editor(ctrl):
    return render_to_string('simp/image_resize.html', {'image': ctrl.image})
    
def get_preset(ctrl, preset):
    if preset == -1:
        dict = Preset().to_dict()
        dict["coords"] = {'w': ctrl.get_image_width(), 'h': ctrl.image.get_image_height()}
        return dict
    else:
        preset = get_preset_model(preset)
        return Preset().from_json(preset.settings).to_dict()
    return None

def apply(ctrl):    
    pass

def commit(ctrl, preset, values, newImg):
    img = Image.open(ctrl.image.get_image_filename())
    size = values["w"], values["h"]
    cropImg = None
    if ctrl.image.get_image_width() < values["w"] and ctrl.image.get_image_height() < values["h"]:
        cropImg = img.resize(size, Image.BICUBIC)
    else:
        cropImg = img.resize(size, Image.ANTIALIAS)
    cropImg.save(newImg.get_image_filename())
    newImg.desc = "resized imagefile"
    return newImg 

def get_preset_model(preset):
    preset = None
    if preset > 0:
        preset = Preset.objects.get(module=Module.objects.get(label="crop"), order=preset)
    return preset;
